package edu.neu.swc.cloud.controller;

import com.alibaba.fastjson.JSON;
import edu.neu.swc.cloud.domain.ConfigPro;
import edu.neu.swc.cloud.domain.Equipment;
import edu.neu.swc.cloud.domain.Factory;
import edu.neu.swc.cloud.domain.Product;
import edu.neu.swc.cloud.domain.QueryInfo;
import edu.neu.swc.cloud.domain.Rent;
import edu.neu.swc.cloud.service.EquipmentService;
import edu.neu.swc.cloud.service.FactoryService;
import edu.neu.swc.cloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/myEquip")
public class MyEquipController {

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    ProductService productService;

    @Autowired
    FactoryService factoryService;

    @RequestMapping("/myEquipList")
    public String equipList(QueryInfo queryInfo){
        int numbers = equipmentService.getMyEquipCounts("%" + queryInfo.getQuery() + "%",queryInfo.getFac());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Equipment> equipmentList = equipmentService.findMyEquipByQuery("%"+queryInfo.getQuery()+"%",queryInfo.getFac(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",equipmentList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/getRent")
    public String getRent(int id){
        Rent rent = new Rent();
        Map<String,Object> res = new HashMap<>();
        Equipment equipment = equipmentService.findEquipById(id);
        if(equipment.getEq_rent() == 0){
            //该设备可租用
            rent.setRent_equ_id(equipment.getEq_id());
            res.put("rent",rent);
            res.put("flag","ok");
        }else{
            res.put("flag","error");
        }
        System.out.println(res);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/confirmRent")
    public String confirmRent(@RequestBody Rent rent){
        equipmentService.rentEquip(rent);
        return "success";
    }

    @RequestMapping("/returnEquip")
    public String returnEquip(int id){
        Equipment equipment = equipmentService.findEquipById(id);
        if(equipment.getEq_rent() != -1){
            equipmentService.returnBack(id);
            return "success";
        }
        return "error";
    }

    @RequestMapping("/getConfigList")
    public String getConfigList(QueryInfo queryInfo){
        System.out.println(queryInfo);
        int numbers = equipmentService.getConfigCounts("%" + queryInfo.getQuery() + "%",queryInfo.getFac(),queryInfo.getEqu());
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<ConfigPro> configList = equipmentService.findConfigByQuery("%" + queryInfo.getQuery() + "%",queryInfo.getFac(),queryInfo.getEqu(),pageStart,queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",configList);
        System.out.println("总条数："+numbers);
        System.out.println(configList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/addConfig")
    public String addConfig(@RequestBody ConfigPro configPro){
        equipmentService.AddConfig(configPro);
        return "success";
    }

    @RequestMapping("/allMyEquipList")
    public String allMyEquipList(int id){
        System.out.println("ord_id: " + id);
        List<Equipment> equipmentList = equipmentService.findEquipByFacId(id);
        Map<String,Object> res = new HashMap<>();
        res.put("data",equipmentList);
        System.out.println(equipmentList);
        return JSON.toJSONString(res);
    }

    @RequestMapping("/addMyEquip")
    public String addMyEquip(@RequestBody Equipment equipment){
        equipmentService.addFacEquip(equipment);
        System.out.println(equipment);
        return "success";
    }
}
